﻿<!DOCTYPE html>
<html lang="zh_CN">

<head>
    <title>移动的小球(渐变)</title>
    <meta charset="UTF-8">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="图形系统开发实战:进阶篇 示例">
    <meta name="author" content="hjq">
    <meta name="keywords" content="canvas,anygraph,javascript">
    <!-- frame所需脚本和样式 -->
    <link rel="stylesheet" href="../../script/bootstrap-3.3.5/css/bootstrap.min.css">   
    <link rel="stylesheet" href="../../script/codemirror/codemirror.css">
    <script src="../../script/lib/jquery-1.11.2.min.js"></script>
    <script src="../../script/codemirror/codemirror_merge.js"></script>
    <!-- 加载代码块 -->
	<link rel="stylesheet" href="../css/styles.css">
    <script src="../js/helper.js"></script>
    <script>
        var pageTitle = "缓动-移动的小球";
        var pageDesc = "使用缓动效果，实现小球的移动";
    </script>

    <script type="module">
        import { Graph, VectorSource, Layer, Text, Circle, BgUtil, Animation, Easing, MathUtil, Color, Gradient } from "../../src/index.js";

        // 初始化graph对象
        let graph = new Graph({
            "target": "graphWrapper"
        });

        // 定义小球属性
        let defs = [
            { "x": 50, "y": 50, "radius": 20, "name": "easeIn", "easing": Easing.easeIn },
            { "x": 50, "y": 100, "radius": 20, "name": "easeOut", "easing": Easing.easeOut },
            { "x": 50, "y": 150, "radius": 20, "name": "inAndOut", "easing": Easing.inAndOut },
            { "x": 50, "y": 200, "radius": 20, "name": "linear", "easing": Easing.linear },
            { "x": 50, "y": 250, "radius": 20, "name": "upAndDown", "easing": Easing.upAndDown },
            { "x": 50, "y": 300, "radius": 20, "name": "easeInSine", "easing": Easing.easeInSine },
            { "x": 50, "y": 350, "radius": 20, "name": "easeOutSine", "easing": Easing.easeOutSine },
            { "x": 50, "y": 400, "radius": 20, "name": "easeInOutQuint", "easing": Easing.easeInOutQuint }
        ];


        // 显示辅助网格
        let bgLayer = BgUtil.generateGrid(Object.assign({ "interval": 10, "graph": graph }, graph.getSize()));
        bgLayer.getSource().add(new Text({
            "text": "缓动 Demo",
            "x": graph.getSize().width / 2,
            "y": graph.getSize().height / 2,
            "vectorSize": false,
            "style": { "lineWidth": 4, "fillStyle": 0, "fillColor": "#D0D0D0", "fontSize": 30, "fontName": "黑体", "textAlign": "center", "textBaseline": "middle" }
        }));

        // 增加数据层
        let layer = graph.addLayer({ "name": "数据层" });
        let balls = [];
        let colorSet = ["#FF0000", "#FFAA00", "#FFFF00", "#00FF00", "#00FFFF", "#0000FF", "#000000"];

        for (let i = 0; i < defs.length; i++) {
            let colorBand = Color.band(colorSet[i % colorSet.length], 10);
            let gradient = new Gradient({
                "type": "radial",
                "coords": { "x1": 0.7, "y1": 0.7, "r1": 0, "x2": 0.7, "y2": 0.7, "r2": 0.8 },
                "gradientUnits": "percentage",
                "colorStops": [{
                    "offset": "0.1",
                    "color": colorBand[2]
                }, {
                    "offset": "0.95",
                    "color": colorBand[7]
                }]
            });

            balls.push({
                "easing": defs[i].easing,
                "circle": layer.getSource().add(
                    new Circle({
                        "x": defs[i].x,
                        "y": defs[i].y,
                        "radius": defs[i].radius,
                        "style": { "color": "none", "fillStyle": 1, "fillColor": gradient }
                    }))
            });

            layer.getSource().add(
                new Text({
                    "text": defs[i].name,
                    "x": defs[i].x + defs[i].radius * 2,
                    "y": defs[i].y,
                    "style": { "fontSize": 24, "fillColor": "blue", "fontName": "Verdana", "textBaseline": "middle" }
                }))
        }

        let running = false;
        let times = 0;
        let totalTimes = 120;
        let minX = 50, maxX = 800;

        function frame() {
            for (let i = 0, len = balls.length; i < len; i++) {
                let dx = MathUtil.lerp(minX, maxX, balls[i].easing(times / totalTimes));
                balls[i].circle.moveTo(dx, defs[i].y);
            }
            // 图形渲染
            graph.render();
            if (running === true && times < totalTimes) {
                Animation.frame(frame);
            }
            times++;
        }
        $("#btnStart").on("click", function () {
            times = 0;
            running = true;
            Animation.frame(frame);
        });
        $("#btnStop").on("click", function () {
            running = false;
        })
    </script>
</head>

<body style="margin:10px;">
    <div id="graphWrapper" data-type="graph" style="width:850px; height:450px; border:solid 1px #CCC;"></div>
    <div style="margin:20px">
        <button class="btn btn-success" id="btnStart">开始</button>
        <button class="btn btn-success" id="btnStop">停止</button>
    </div>
</body>

</html>